<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Question as UM;
use app\admin\validate\ProblemValidate;
use think\Db;
use think\Request;

class Question extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 列表
    */
    public function index()
    {
        
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            if(!$model->_checkName($request['cate_title'])){
                die(outputError($model->getError()));
            }

            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $big_cate = Db::name('course_cate')->select();
            $this->assign([
                'big_cate'=>$big_cate,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $id = (int)$this->request->get('id');

            
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $big_cate = Db::name('course_cate')->select();
            $this->assign([
                'data'=>$data,
                'big_cate'=>$big_cate,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');

            //查看是否有子章节
            $is_have = \app\admin\model\ChapterDetail::where('cate',$id)->find();
            if($is_have){
                die(outputError('请先删除试题以及章节'));
            }
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
   
    
    /**
    * 设置管理状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new UM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    
    /**
    * 查看设置详情
    */
    public function detail(){
        $id = (int)$this->request->get('id');
        
        $model = new UM();
        $info = $model->_findOne($id);
        $this->assign([
            'info' => $info
        ]);
        
        return $this->fetch();
    }


    //试题上传处理
    public function bath_one(Request $request)
    {
            //获取表单上传文件
            $file = $request->file('file');
            $info = $file->validate(['size'=>1024*1024,'ext'=>'xlsx,xls,csv'])->move( './upload/excel');
            $exclePath = $info->getSaveName();  //获取文件名
            $file_name =   './upload/excel/'. $exclePath;   //上传文件的地址
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
            $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
            $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);
            $data = [];
            $i = 0;
            $error = 0;
            foreach($excel_array as $k=>$v) {
                if (empty($v[6])){
                    continue;
                }
                $i++;
                unset($data[$k]['id']);
                $data[$k]['num'] = $v[0];
                $data[$k]['title'] = $v[1];
                $data[$k]['A'] = $v[2];
                $data[$k]['B'] = $v[3];
                $data[$k]['C'] = $v[4];
                $data[$k]['D'] = $v[5];
                $data[$k]['sites'] = $v[6];
                $data[$k]['answer'] = $v[7];
                $data[$k]['analysis'] = $v[8];
                $data[$k]['is_free'] = empty($v[9])?0:$v[9];
                $data[$k]['mark'] = empty($v[10])?:$v[10];
            }
    }
}
